<?php

    $_ECLIPSEMDE = array();
    $_ECLIPSEMDE['TEMPLATING'] = false;
    require_once('../../SiteIncludes.GLOBAL.php');
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	$gVAR['e'] = '404';
	require_once('error_handler.php');
	die();
    }
        
        $return_head = <<<EOHEAD
	<!DOCTYPE html>
	    <html>
	    <head><title></title></head>
	    <body>
		<script type="text/javascript">
EOHEAD;
    $return_nok = 'parent.EDJXConfirmer.show_nok(); ';
    $return_ok = 'parent.EDJXConfirmer.show_ok(); ';
    $return_body = '';
    $return_foot = <<<EOFOOT
		</script>
	    </body>
	</html>
EOFOOT;
    
    // Ensure all variables needed are set
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($pVAR['search_user_name']) === true) {
        $safe_username = '%'.$sql_conx->real_escape_string($pVAR['search_user_name']).'%';
        $query = $sql_conx->query("SELECT DISTINCT `uid` FROM `users` WHERE `username` LIKE '{$safe_username}' LIMIT 15;");
        if($query !== false && $query->num_rows > 0) {
            while($row = $query->fetch_assoc()) {
		$item =& $UserStash->stash((int)$row['uid']);
                $date = '&nbsp;';
		$title = $item->get_username();
		$url = EclipseMDE::userLink($item->get_username());
		$description = '&nbsp';//$item->getDescription();
		$icon = $item->get_profile_icon();
		$announcements_feed_items[] = $Templater->makecontentfeeditem($date, $title, $url, $description, $icon);
            }
            $callback_unescaped = ($Templater->maincontent_userlist($announcements_title, $announcements_url, $announcements_feed_items, ''));
            if(isset($pVAR['adm_srch']) === true && isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && $_LOGGEDINUSER->get_priv_adm() === true) {
                $pattern = '/\<a class=\"btn\" href=\"\/user\/(.*?)\">View &raquo;\<\/a\>/i';
                $replacement = "<a class=\"btn\" href=\"/user/$1\">View &raquo;</a> <a class=\"btn\" href=\"/admin/user/$1\">Edit &raquo;</a>";
                $callback_unescaped = preg_replace($pattern, $replacement, $callback_unescaped);
            }
            $callback_unescaped .= <<<EOMORE
                <div class="row-fluid">
                    <div class="span12" style="text-align: center; font-weight: bold; font-style: italic;">Not Here?<br />Try narrowing your search.</div>
                </div>
EOMORE;
        } else {
            $callback_unescaped = <<<EOERROR
		<div class="row-fluid">
		    <div class="span12" style="text-align: center; font-weight: bold; font-style: italic;">Sorry<br/>No Search results found.</div>
		</div>
EOERROR;
        }
        $callback_escaped = json_encode($callback_unescaped);
	$return_body = $return_ok.' parent.UserSearchBox.callback('.$callback_escaped.');';
    } else {
        $return_body = $return_nok.'  parent.UserSearchBox.callback(\'\');';
    }
    
    echo $return_head.$return_body.$return_foot;
?>
